package com.heima.behavior.service.impl;

import com.alibaba.fastjson.JSON;
import com.heima.behavior.model.dto.LikesBehaviorDto;
import com.heima.behavior.model.pojo.ApLikesBehavior;
import com.heima.behavior.service.ApLikesBehaviorService;
import com.heima.behavior.utils.AppThreadLocalUtil;
import com.heima.common.constants.KafkaMessageConstants;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.message.UpdateArticleMess;
import org.bson.types.ObjectId;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

@Service
public class ApLikesBehaviorServiceImpl implements ApLikesBehaviorService {

    @Resource
    private MongoTemplate mongoTemplate;

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Resource
    private KafkaTemplate kafkaTemplate;

    @Override
    public ResponseResult like(LikesBehaviorDto dto) {
        //1.参数校验
        if (dto == null || dto.getArticleId() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        //2.判断用户是否登录
        Long userId = AppThreadLocalUtil.getId();
        if (userId == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }
        //3.判断是否已经点赞
        String like_hash_key = "USER_LIKES_" + dto.getArticleId();
        Boolean hasKey = stringRedisTemplate.opsForHash().hasKey(like_hash_key, userId + "");
        //3.1已经点赞-- 点赞
        if (hasKey && dto.getOperation() == 0) {
            return ResponseResult.errorResult(AppHttpCodeEnum.SUCCESS);
        }
        //3.2已经点赞 --取消点赞
        if (hasKey && dto.getOperation() == 1) {
            //3.2.1删除缓存数据
            stringRedisTemplate.opsForHash().delete(like_hash_key, userId + "");
            //3.2.2删除mongodb数据
            Query query = Query.query(Criteria.where("userId").is(userId).and("articleId").is(dto.getArticleId()));
            mongoTemplate.remove(query, ApLikesBehavior.class);
        }
        //3.3未点赞 --点赞
        if (!hasKey && dto.getOperation() == 0){
            //3.3.1保存数据到redis
            stringRedisTemplate.opsForHash().put(like_hash_key,userId + "","1");
            //3.3.2保存数据到mongodb
            ApLikesBehavior apLikesBehavior = new ApLikesBehavior();
            apLikesBehavior.setType(dto.getType());
            apLikesBehavior.setArticleId(dto.getArticleId());
            apLikesBehavior.setUserId(userId);
            apLikesBehavior.setCreatedTime(new Date());
            apLikesBehavior.setId(ObjectId.get().toHexString());
            mongoTemplate.insert(apLikesBehavior);
        }

        //发送行为消息
        UpdateArticleMess mess = new UpdateArticleMess();
        mess.setAdd(dto.getOperation() == 0 ? 1 : -1);
        mess.setArticleId(dto.getArticleId());
        mess.setType(UpdateArticleMess.UpdateArticleType.LIKES);

        kafkaTemplate.send(KafkaMessageConstants.HOT_ARTICLE_SCORE_TOPIC, JSON.toJSONString(mess));

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
